cap log close
clear
set mem 200m
set more off
#delimit;
cd "/Users/willafriedman/Dropbox/a_research/gsp/replication";

use edaslib.dta;

/* START LOG FILE */;
log using edaslib2015replication, text replace; 


estimates clear;

/***************************************
*CLEANING AND GENERATING VARIABLES******
***************************************/


/**********SAMPLE WEIGHTS*******/
gen weight=1;
replace weight=5.03 if (lot=="H" | lot=="I" | lot=="J"); /*Intensives were 1/5.03 of remaining sample*/;
replace weight=5.03 if intensive_sample==1; 

/***HIGHEST STANDARD***/
gen s3_1highesteduclev_07=s3_1highesteduclev;
/* Reduce highest standard by one for those who got more by being interviewed later? */;
replace s3_1highesteduclev_07=s3_1highesteduclev_07-1 if s1_7date3==2007 & s3_afullyear6==1;
summ s3_1highesteduclev [aweight=weight];
bys treatment: summ s3_1highesteduclev [aweight=weight];
summ s3_1highesteduclev_07 [aweight=weight];
bys treatment: summ s3_1highesteduclev_07 [aweight=weight];

/*INDICATOR VARIABLE FOR STILL ATTENDING SCHOOL*/
gen s3_inschool=.;
replace s3_inschool=1 if (s3_aattendsch2==1 & (s1_7date3==2006|s1_7date3==2007))|(s3_battendsch2==1 & s1_7date3==2005);
replace s3_inschool=0 if (s3_aattendsch2==2 & (s1_7date3==2006|s1_7date3==2007))|(s3_battendsch2==2 & s1_7date3==2005);
tab s3_inschool [aweight=weight];
bys treatment: tab s3_inschool [aweight=weight];

/*ATTENDING ANY SECONDARY SCHOOL*/
gen anysec=.;
replace anysec = 0 if quizid~=.;
replace anysec=1 if (s3_1highesteduclev>8 & s3_1highesteduclev~=.);
foreach var in a b c d e f {;
replace anysec=1 if s3_`var'level5>8 & s3_`var'level5~=.;
};

replace anysec=. if (s3_1highesteduclev==. & s3_alevel5==. & s3_blevel5==. & s3_clevel5==.
	& s3_dlevel5==. & s3_elevel5==. & s3_flevel5==.);
tab anysec [aweight=weight]; bys treatment: tab anysec [aweight=weight];



/*SELF PERCEIVED HAPPINESS*/
gen s15_happy=.;
replace s15_happy=1 if s15_10together==1;
replace s15_happy=0 if (s15_10together==2 | s15_10together==3);
replace s15_happy=. if s15_10together==.;
label var s15_happy "Index of Self-Perceived Happiness";
replace s15_happy=. if s15_10together==99;
tab s15_happy [aweight=weight];
bys treatment: tab s15_happy [aweight=weight];

/*MARRIAGE*/
gen s18_marriage=.;
replace s18_marriage=1 if s18_1married==1;
replace s18_marriage=0 if s18_1married==2;
tab s18_marriage;
bys treatment: tab s18_marriage;

/*FERTILITY*/
gen s19_fertility=.;
label var s19_fertility "=1 if Self/Partner ever been pregnant";
replace s19_fertility=0 if s19_1pregnant==2;
replace s19_fertility=1 if (s19_1pregnant==1 | s19_2miscarriage==1);
tab s19_fertility [aw=weight];
bys treatment: tab s19_fertility [aw=weight];
gen s19_total_fert=s19_3times;
replace s19_total_fert=0 if s19_fertility==0;

/*COGNITIVE TESTS*/
gen raven=s4_raven_atotal+s4_raven_btotal+s4_raven_abtotal;
label var raven "Sum of all Raven Tests";

gen englishvocab=0;
gen swahilivocab=0;
foreach var of numlist 1/15{;
 replace englishvocab=englishvocab+1 if s4_english_`var'==1;
 replace swahilivocab=swahilivocab+1 if s4_swahili_`var'==1;
};
replace englishvocab=. if quizid==.;
replace swahilivocab=. if quizid==.;
gen vocab=englishvocab+swahilivocab;
label var englishvocab "English Vocabulary Test: total correct answers (out of 15)";
label var swahilivocab "Swahili Vocabulary Test: total correct answers (out of 15)";
label var vocab "Total Vocabulary Score: English + Swahili (out of 30)";

/* CLEAN YEAR OF BIRTH VARIABLE */;
replace year_of_birth = . if (year_of_birth>1992 | year_of_birth<1980);
summ year_of_birth [aw=weight];
bys treatment: summ year_of_birth [aw=weight];

/*fix year of survey for cohort control*/
replace s1_7date3=2006 if quizid==2470;

gen survey2005=.;
replace survey2005=0 if quizid~=.;
replace survey2005=1 if s1_7date3==2005;
label var survey2005 "Surveyed in 2005"; tab survey2005 [aw=weight];
bys treatment: tab survey2005 [aw=weight];

gen survey2006=.;
replace survey2006=0 if quizid~=.;
replace survey2006=1 if s1_7date3==2006;
label var survey2006 "Surveyed in 2006"; tab survey2006 [aw=weight];
bys treatment: tab survey2006 [aw=weight];

gen survey2007=.;
replace survey2007=0 if quizid~=.;
replace survey2007=1 if s1_7date3==2007;
label var survey2007 "Surveyed in 2007";
tab survey2007 [aw=weight];
bys treatment: tab survey2007 [aw=weight];

gen survey2008=.;
replace survey2008=0 if quizid~=.;
replace survey2008=1 if s1_7date3==2008;
label var survey2008 "Surveyed in 2008";
tab survey2008 [aw=weight];
bys treatment: tab survey2008 [aw=weight];

gen survey2009=.;
replace survey2009=0 if quizid~=.;
replace survey2009=1 if s1_7date3==2009;
label var survey2009 "Surveyed in 2009";
tab survey2009 [aw=weight];
bys treatment: tab survey2009 [aw=weight];

/*fix school_baseline*/
replace school_baseline=306 if quizid==1155;


/* SOCIAL CAPITAL / COMMUNITY GROUP INDEX */;
gen sk_group_index = (s12_1womengrp==1) + (s12_2farmgrp==1) + (s12_3youthgrp==1)
	+ (s12_4watergrp==1) + (s12_5creditins==1) + (s12_6biblegrp==1)
	+ (s12_7burialcom==1) + (s12_8schcom==1) + (s12_9sports==1) + (s12_10commgrp==1);
summ sk_group_index [aw=weight];
bys treatment: summ sk_group_index [aw=weight];

/* TRUST */;
gen sk_trust = (s12_11==1);
replace sk_trust=. if quizid==.;
bys treatment: summ sk_trust [aw=weight];

/* MEDIA EXPOSURE: s12_16radio, s12_17newspaper */;
tab s12_16radio [aw=weight];
bys treatment: tab s12_16radio [aw=weight];
tab s12_17newspaper [aw=weight];
bys treatment: tab s12_17newspaper [aw=weight];

/* ECONOMY IS IMPROVING (RELATIVE TO TWO YEARS AGO) */;
gen s12_19_econ_improve = (s12_19economic==1);
replace s12_19_econ_improve=. if quizid==.;
bys treatment: tab s12_19_econ_improve [aw=weight];

/* QUALITY OF GOVERNMENT IS IMPROVING (RELATIVE TO TWO YEARS AGO) */;
gen s12_20_gov_improve = (s12_20government==1);
replace s12_20_gov_improve=. if quizid==.;
bys treatment: tab s12_20_gov_improve [aw=weight];

/* INTERESTED IN PUBLIC AFFAIRS */;
gen s12_23_interested = (s12_23politicsparticipation==1 | s12_23politicsparticipation==2);
replace s12_23_interested=. if quizid==.;
tab s12_23_interested [aw=weight];
bys treatment: tab s12_23_interested [aw=weight];

/* VOTING INTENTIONS */;
gen s12_26vote2007_restrict = (s12_26vote2007==1);
replace s12_26vote2007_restrict=. if (s12_26vote2007>2 | quizid==.);
tab s12_26vote2007_restrict [aw=weight];
bys treatment: tab s12_26vote2007_restrict [aw=weight];

/* DEMOCRATIC ATTITUDES */;
gen s12_39_reverse = (5 - s12_39fairelection)/4 if s12_39fairelection~=99;
gen s12_40_democratic = (s12_40==1) if s12_40~=99 & quizid~=.;
gen s12_41_satisfied = (4 - s12_41)/3 if s12_41~=99;

/* ETHNIC, RELIGIOUS ATTACHMENTS */;
gen s13_12_tribe_important = (s13_12ethnicimp==1);
replace s13_12_tribe_important=. if quizid==.;
tab s13_12_tribe_important [aw=weight];
bys treatment: tab s13_12_tribe_important [aw=weight];

gen s13_13_religion_important = (s13_13religion==1);
replace s13_13_religion_important=. if quizid==.;
tab s13_13_religion_important [aw=weight];
bys treatment: tab s13_13_religion_important [aw=weight];

/* PARENT EDUCATION, DEMEANED */;

gen feduc=s5_12educlevel if s5_12educlevel<26;
gen meduc=s5_21educlevel if s5_21educlevel<26;

/* CONSTRUCT WIN / RD VARIABLES */;
gen win=1 if (winner_2001==1 | winner_2002==1);
replace win=0 if win==.;
replace win=. if quizid==.;
label var win "Won Scholarship";
bys treatment: summ win [aw=weight];

compress;

replace surveyed=0 if surveyed!=1;

# delimit cr;

gen c1_score_2000=score_2000 if c1==1
sum c1_score_2000 if treatment==0&male==0&teso==0&surveyed==1&s1_7date3<2008
replace c1_score_2000=(c1_score_2000-r(mean))/r(sd)

gen age_01=2001-year_of_birth

gen daded=bs_feduc if bs_feduc<26
gen momed=bs_meduc if bs_meduc<26

replace bs_flang=1 if bs8_60=="BUKUSU"|bs8_60=="KIBUGUSU"|bs8_60=="KIBUKUSI"|bs8_60=="KIBUKUSU"|bs8_60=="KIISUKHA"|bs8_60=="KIKHAYO"|bs8_60=="KILUHYA"|bs8_60=="BANYORO"|bs8_60=="KILUYIA"|bs8_60=="KIMARACHI"|bs8_60=="KIMARACHI (LUHYA)"|bs8_60=="KIMARAGOLI"|bs8_60=="KISAMIA"|bs8_60=="KISUKHA"|bs8_60=="KITIRIKI"|bs8_60=="KIWANGA"|bs8_60=="LUHYIA"|bs8_60=="LUHYA"|bs8_60=="MARACHI"|bs8_60=="MARAGOLI"|bs8_60=="MNYORE"|bs8_60=="MULUHYA"|bs8_60=="MULUYA"|bs8_60=="MUNYORE"|bs8_60=="MUWANGA"|bs8_60=="MWANGA"|bs8_60=="TIRIKI"|bs8_60=="TIRIKI LUHYA"|bs8_60=="WANGA"|bs8_60=="KINYOLE"|bs8_60=="KINYORE"|bs8_60=="KINYALA"|bs8_60=="MARAMA"|bs8_60=="ITAKHO"|bs8_60=="KABRASI"|bs8_60=="MKISA"
replace bs_flang=2 if bs8_60=="TESO"|bs8_60=="KITESO"
replace bs_flang=3 if bs8_60=="KILUO"|bs8_60=="JALUO"

gen tribe_teso=1 if bs_flang==2
replace tribe_teso=0 if bs_flang!=.&bs_flang!=2

gen tribe_luhya=1 if bs_flang==1
replace tribe_luhya=0 if bs_flang!=.&bs_flang!=1

gen tribe_luo=1 if bs_flang==3
replace tribe_luo=0 if bs_flang!=.&bs_flang!=3

gen ever_migrated=1 if s14_1migrated==1
replace ever_migrated=0 if s14_1migrated==2|s14_1migrated==3

quietly gen comppart = part_2001_v1 if std_baseline==6
quietly replace comppart = part_avg_2002 if std_baseline==5

gen age_survey=age_01+8 if survey2009==1
replace age_survey=age_01+7 if survey2008==1
replace age_survey=age_01+6 if survey2007==1
replace age_survey=age_01+5 if survey2006==1
replace age_survey=age_01+4 if survey2005==1

rename s12_31ugandapresident s12_31ugpres
rename s12_28vice_president s12_28vp
rename s12_29educminister s12_29edmin
rename s12_30healthminister s12_30healthmin

foreach p in 27president 28vp 29edmin 30healthmin 31ugpres{
	gen s12_`p'_nm=1 if s12_`p'==1
	replace s12_`p'_nm=0 if s12_`p'==2|s12_`p'==99
	replace s12_`p'_nm=. if s12_`p'==.
	replace s12_`p'=0 if s12_`p'==2|s12_`p'==99
}
replace s12_35singleparty=. if s12_35singleparty==99

gen supportforviolence1=(s12_42-1)/3 if s12_42!=5
replace supportforviolence1=.5 if s12_42==5
gen supportforviolence2=1 if s12_42==3|s12_42==4
replace supportforviolence2=0 if s12_42==1|s12_42==2

gen insample=1 if arithmetic_1stfo!=.&englishvocab!=.&raven!=.&reading_correct_1stfo!=.&swahilivocab!=.&ever_migrated!=.&s3_inschool!=.&anysec!=.&s3_1highesteduclev_07!=.&s18_marriage!=.&s19_fertility!=.&s15_happy!=.
foreach p in s12_27president s12_28vp s12_29edmin s12_30healthmin s12_31ugpres s12_20_gov_improve s12_19_econ_improve s12_41_satisfied s12_43 supportforviolence1 s12_23_interested s12_17newspaper s12_16radio s12_39_reverse s12_40_democratic s12_44 s12_33 s12_34 s12_35 s12_36 s12_37 s12_38 s12_45 s12_46 c1 age_survey survey2005 survey2007 meduc feduc{
	replace insample=. if `p'==.
}
count if insample==1

gen compscore=score_2001 if std_baseline==6
replace compscore=score_2002 if std_baseline==5

sum compscore if treatment==0&insample==1 [aw=weight]
replace compscore=(compscore-r(mean))/r(sd)

sum score_2000 if treatment==0&insample==1 [aw=weight]
replace score_2000=(score_2000-r(mean))/r(sd)

foreach var in reading_correct_1stfo arithmetic_1stfo swahilivocab englishvocab raven vocab{
	sum `var' if treatment==0&insample==1 [aw=weight]
	gen `var'_nm=(`var'-r(mean))/r(sd)
}

egen all_tests=rmean(reading_correct_1stfo_nm arithmetic_1stfo_nm swahilivocab_nm englishvocab_nm raven_nm)
sum all_tests if treatment==0&insample==1 [aw=weight]
gen all_tests_nm=(all_tests-r(mean))/r(sd)

*******

/*Left Busia-Teso*/
gen left_bt=1 if s2_9country!=1
replace left_bt=1 if s2_10adistrict!=1&s2_10adistrict!=2
replace left_bt=1 if s14_5r1district==3|s14_5r1district==4
replace left_bt=1 if s14_10r2district==3|s14_10r2district==4
replace left_bt=1 if s14_15r3district==3|s14_15r3district==4
replace left_bt=0 if left_bt==.&surveyed==1

/*Favorite News Source*/
gen fav_news_none=(s12_18bestnewspaper==0)
gen fav_news_taifa=(s12_18bestnewspaper==1)
gen fav_news_nation=(s12_18bestnewspaper==2)
gen fav_news_standard=(s12_18bestnewspaper==3)
gen fav_news_other=(s12_18bestnewspaper==5)

foreach p in none taifa nation standard{
	replace fav_news_`p'=. if s12_18bestnewspaper==.
}

gen fav_news_not_none=1-fav_news_none

gen s13_12_tribe_import_r=1-s13_12_tribe_important
gen s13_13_relig_import_r=1-s13_13_religion_important

gen married_ind=1 if s18_5apartnerchoice==1
replace married_ind=0 if insample==1&married_ind==.

gen family_choice_unc=1 if s18_5apartnerchoice==2|s18_5apartnerchoice==3
replace family_choice_unc=0 if family_choice_unc==.&insample==1

replace s12_6biblegrp=0 if s12_6biblegrp==2

replace s12_44=2.5 if s12_44==5
replace s12_46=2.5 if s12_46==5

replace s12_43=(s12_43-1)/3
replace s12_44=(s12_44-1)/3
replace s12_34=. if s12_34==99
replace s12_33=(s12_33-1)/4
replace s12_34=(s12_34-1)/4
replace s12_35=(s12_35-1)/4
replace s12_36=(s12_36-1)/4
replace s12_37=(s12_37-1)/4
replace s12_38=(s12_38-1)/4
replace s12_45=(s12_45-1)/3
replace s12_46=(s12_46-1)/3

gen s12_44_rev=1-s12_44
gen s12_45_rev=1-s12_45
gen s12_46_rev=1-s12_46


************************
***Table 1:
***********************
***panels A&B:

foreach lhs in age_01 bs1_9iron momed daded tribe_teso tribe_luhya tribe_luo score_2000 compscore comppart{
	sum `lhs' if treatment==0&insample==1 [aw=weight]
}

foreach lhs in age_01 bs1_9iron momed daded tribe_teso tribe_luhya tribe_luo score_2000 compscore comppart{
	reg `lhs' treatment if insample==1 [aw=weight], cluster(school_baseline)
}	

preserve

use edaslib_teacherattendance, clear

sum actatten if (tchposition==3|tchposition==4)&teso==0&treat==0
reg actatten treat if (tchposition==3|tchposition==4)&teso==0, cluster(schid)	
restore

*********************;

********************************
*Table 2: 
********************************

local controls="c1 age_survey survey2005 survey2007 meduc feduc"

*******************
***mean effects for test scores

local tests="arithmetic_1stfo_nm englishvocab_nm raven_nm reading_correct_1stfo_nm swahilivocab_nm"

foreach var in `tests'{
	sum `var' if treatment==0&insample==1 [aw=weight]
	replace `var'=(`var'-r(mean))/r(sd)
}

egen depvar=rmean(`tests') if surveyed==1
sum depvar if treat==0&insample==1 [aw=weight]
replace depvar=(depvar-r(mean))/r(sd)

sum depvar if treat==0&insample==1 [aw=weight]
xi:  reg depvar treatment `controls' if insample==1 [aweight=weight], robust cluster(school_baseline)

******************
*****Restricted mean effects

*************RESTRICTED TO <+2 S.D (not renormalized)
sum depvar if treat==0&insample==1 &score_2000<2 [aw=weight]
xi:  reg depvar treatment `controls' if score_2000<2 [aweight=weight], robust cluster(school_baseline)

*************RESTRICTED TO <=5 WINNERS (not re-normalized)

foreach p in 10 20 30 40 50 60 70 80 90{
	bysort school_baseline: egen base_score_`p'=pctile(c1_score_2000), p(`p')
}

bysort school_baseline: egen winners=sum(win)
egen tag_school=tag(school_baseline)
*one obs per school so as not to over-weight large schools

tobit winners base_score* if treatment==1&tag==1&teso==0, ll(0)
predict winners_predicted

tab winners_predicted
replace winners_predicted=0 if winners_predicted<0

sum depvar if treat==0&insample==1&winners_predicted<=5 [aw=weight]
xi:  reg depvar treatment `controls' [aweight=weight] if winners_predicted<=5, robust cluster(school_baseline)

drop tag_school winners winners_predicted  base_score*



***Panel A
gen surveyed_old=surveyed
replace surveyed=0 if s1_7date3>2007

sum surveyed if treatment==0&teso==0&gsp_student_baseline==1&school_pulled_out==0& (surveyed==1 | intensive_sample==1|died==1|refused==1) [aw=weight]
reg surveyed treatment if teso==0&gsp_student_baseline==1&school_pulled_out==0& (surveyed==1 | intensive_sample==1|died==1|refused==1) [aw=weight], cluster(school_baseline)	

gen found=(surveyed==1|refused==1|died==1)
replace found=0 if s1_7date3>2007&surveyed==1
sum found if treatment==0&teso==0&gsp_student_baseline==1&school_pulled_out==0& (found==1 | intensive_sample==1) [aw=weight]
reg found treatment if teso==0&gsp_student_baseline==1&school_pulled_out==0& (found==1 | intensive_sample==1) [aw=weight], cluster(school_baseline)	

sum found if teso==0&gsp_student_baseline==1&school_pulled_out==0& (found==1 | intensive_sample==1) [aw=weight]

***Panels B & C

local table2lhs="arithmetic_1stfo_nm englishvocab_nm raven_nm reading_correct_1stfo_nm swahilivocab_nm anysec s3_inschool s3_1highesteduclev_07"
	
foreach lhs in `table2lhs'{
	sum `lhs' if treatment==0&insample==1 [aw=weight]
}	
foreach lhs in `table2lhs'{
	reg `lhs' treatment `controls' if insample==1 [aw=weight], robust cluster(school_baseline)
}


********************************
*Tables 3-6: 
********************************
//COMMENT: OLS regressions on mean effect are done later on (section: Table 7)

local controls="c1 age_survey survey2005 survey2007 meduc feduc "

local wom = "s12_45_rev s12_46_rev s18_marriage family_choice_unc married_ind s19_total_fert"
local ethnic= "s13_12_tribe_import_r left_bt s13_13_relig_import_r s12_6biblegrp"
local dem="s12_39_reverse s12_40_democratic s12_44_rev s12_35 s12_36 s12_37 s12_38 " 
local sources="s12_16radio s12_17newspaper fav_news_not_none fav_news_nation"
local polknowledge="s12_27president s12_28vp s12_29edmin s12_30healthmin s12_31ugpres" 
local sat="s12_43 s12_20_gov_improve s12_19_econ_improve s12_41_satisfied s15_happy"
local effic="s12_33 s12_34 "
local int="s12_23_interested s12_26vote2007_restrict sk_group_index"
local other="sk_trust supportforviolence1 supportforviolence2"

local table3lhs "`wom'"
local table4lhs="`ethnic' `dem'"
local table5lhs="`sources' `polknowledge' `sat'"
local table6lhs="`effic' `int' `other'"


foreach lhs in `table3lhs' {
	sum `lhs' if treatment==0&insample==1 [aw=weight]
}

foreach lhs in `table3lhs' {
	reg `lhs' treatment `controls' [aw=weight] if insample==1, robust cluster(school_baseline)
}

foreach lhs in `table4lhs' {
	sum `lhs' if treatment==0&insample==1 [aw=weight]
}

foreach lhs in `table4lhs' {
	reg `lhs' treatment `controls' [aw=weight] if insample==1, robust cluster(school_baseline)
}


foreach lhs in `table5lhs' {
	sum `lhs' if treatment==0&insample==1 [aw=weight]
}

foreach lhs in `table5lhs' {
	reg `lhs' treatment `controls' [aw=weight] if insample==1, robust cluster(school_baseline)
}

foreach lhs in `table6lhs' {
	sum `lhs' if treatment==0&insample==1 [aw=weight]
}

foreach lhs in `table6lhs' {
	reg `lhs' treatment `controls' [aw=weight] if insample==1, robust cluster(school_baseline)
}

*****RESTRICTED SAMPLE FOR ETHNIC IDENTITY
gen late=(s1_7date3>2006&s1_7date3!=.)

sum s13_12_tribe_import_r if treatment==0&insample==1&late!=1 [aw=weight]
reg s13_12_tribe_import_r treatment c1 age_survey survey2005 survey2007 meduc feduc if insample==1&late!=1 [aw=weight], robust cluster(school_baseline)


**************************************
*******TABLE 7: mean effects
**************************************

*replace bubs12_46_nm=.5 if s12_46==2.5

local wom="s12_45_rev s12_46_rev family_choice_unc"
local dem="s12_39_reverse s12_40_democratic s12_44_rev s12_35 s12_36 s12_37 s12_38" 
local polknowledge="s12_27president s12_28vp s12_29edmin s12_30healthmin s12_31ugpres" 
local sat="s12_43 s12_20_gov_improve s12_19_econ_improve s12_41_satisfied"
local effic="s12_33 s12_34"
local int="s12_23_interested s12_26vote2007_restrict sk_group_index"


preserve
*****OLS for rows 1, 6, 8, 2, 9, 10
foreach categ in wom dem polknowledge sat effic int{

local cat="``categ''"
local cat_name="`categ'"
foreach var in `cat'{
	sum `var' if treatment==0&insample==1 [aw=weight]
	replace `var'=(`var'-r(mean))/r(sd)
}

egen depvar_`categ'=rmean(`cat') if insample==1
sum depvar_`categ' if treatment==0&insample==1 [aw=weight]
replace depvar_`categ'=(depvar_`categ'-r(mean))/r(sd)

xi: reg depvar_`cat_name' treatment `controls' if insample==1 [aweight=weight], robust cluster(school_baseline)
xi: ivregress 2sls depvar_`cat_name' `controls' (all_tests_nm = treatment) if insample==1 [aw=weight], cluster(school_baseline)
xi: reg depvar_`cat_name' all_tests_nm `controls' if insample==1&treatment==0 [aweight=weight], robust cluster(school_baseline)
}

qui reg all_tests_nm treatment `controls' if insample==1 [aw=weight], cluster(school_baseline)
qui predict resid, resid

*****hausman on mean effects for rows 1, 6, 8, 2, 9, 10\
*NOTE: THE TEST IS THE SIGNIFICANCE OF THE COEF ON resid (look at second line row 'resid'; column 'P>|t|')
foreach cat in wom dem polknowledge sat effic int{
	reg depvar_`cat' all_tests_nm resid `controls' if insample==1 [aw=weight], cluster(school_baseline)
}

***************************
*****OLS for rows 3, 4, 5, 7 
foreach var in s13_12_tribe_import_r s13_13_relig_import_r s12_17newspaper supportforviolence1 supportforviolence2{
	xi: reg `var' treatment `controls' if insample==1 [aweight=weight], robust cluster(school_baseline)
	xi: ivregress 2sls `var' `controls' (all_tests_nm = treatment) if insample==1 [aw=weight], cluster(school_baseline)
	xi: reg `var' all_tests_nm `controls' if insample==1&treatment==0 [aweight=weight], robust cluster(school_baseline)
}
*****hausman on mean effects for rows 3, 4, 5, 7
*NOTE: THE TEST IS THE SIGNIFICANCE OF THE COEF ON resid (look at second line row 'resid'; column 'P>|t|')
foreach var in s13_12_tribe_import_r s13_13_relig_import_r s12_17newspaper supportforviolence1 supportforviolence2{
	reg `var' all_tests_nm resid `controls' if insample==1 [aw=weight], cluster(school_baseline)
}

restore


**************************************
********APPENDIX TABLE A2: 
**************************************


*********ALTERNATIVE WAY (TABLE A1alt)********

preserve

gen insample2=insample
replace insample2=. if score_2000>2

gen insample3=insample
replace insample3=. if score_2000==.

*****OLS for rows 1,4,5,6,7,8

foreach categ in wom dem polknowledge sat effic int{

	local cat="``categ''"
	local cat_name="`categ'"
	foreach var in `cat'{
		sum `var' if treatment==0&insample==1 [aw=weight]
		replace `var'=(`var'-r(mean))/r(sd)
	}

	egen depvar_`categ'=rmean(`cat') if insample==1
	sum depvar_`categ' if treatment==0&insample==1 [aw=weight]
	replace depvar_`categ'=(depvar_`categ'-r(mean))/r(sd)

	xi: reg depvar_`categ' treatment `controls' if insample2==1 [aweight=weight], robust cluster(school_baseline)
	xi: reg depvar_`categ' treatment `controls' if insample3==1 [aweight=weight], robust cluster(school_baseline)
	
}


*****OLS for rows 2,3
foreach var in s13_12_tribe_import_r s13_13_relig_import_r s12_17newspaper supportforviolence1 supportforviolence2{


	xi: reg `var' treatment `controls' if insample2==1 [aweight=weight], robust cluster(school_baseline)
	xi: reg `var' treatment `controls' if insample3==1 [aweight=weight], robust cluster(school_baseline)
	
}

**********************************
*********ANALYSIS FOR THE TEXT****
**********************************
*For the text:
gen bub1=(s12_46==0)
gen bub2=(s12_46==1)
 reg bub1 treatment c1 age_survey survey2005 survey2007 meduc feduc [aw=weight] if insample==1, robust cluster(school_baseline)
 reg bub2 treatment c1 age_survey survey2005 survey2007 meduc feduc [aw=weight] if insample==1, robust cluster(school_baseline)

replace insample2=0 if insample2!=1
gen treatment_select=treatment*insample2

foreach var in depvar_wom depvar_dem depvar_polknowledge depvar_sat depvar_effic depvar_int s13_12_tribe_import_r s13_13_relig_import_r s12_17newspaper supportforviolence1 supportforviolence2{
		xi: reg `var' treatment treatment_select insample2 `controls' [aweight=weight] if insample==1&score_2000!=., cluster(school_baseline)
}


restore



**************************************
********APPENDIX TABLE A1: 
**************************************

local controls="c1 age_survey survey2005 survey2007 meduc feduc"
local controls2="c1 survey2005 survey2007"

preserve
*****OLS for rows 1,4,5,6,7,8
foreach categ in wom dem polknowledge sat effic int{

	local cat="``categ''"
	local cat_name="`categ'"
	foreach var in `cat'{
		sum `var' if treatment==0&insample==1 [aw=weight]
		replace `var'=(`var'-r(mean))/r(sd)
	}

	egen depvar_`categ'=rmean(`cat') if insample==1
	sum depvar_`categ' if treatment==0&insample==1 [aw=weight]
	replace depvar_`categ'=(depvar_`categ'-r(mean))/r(sd)

	xi: reg depvar_`categ' treatment `controls' if insample==1 [aweight=weight], robust cluster(school_baseline)
	xi: reg depvar_`categ' treatment `controls2' if insample==1 [aweight=weight], robust cluster(school_baseline)
	
}


*****OLS for rows 2,3
foreach var in s13_12_tribe_import_r s13_13_relig_import_r s12_17newspaper supportforviolence1 supportforviolence2{


	xi: reg `var' treatment `controls' if insample==1 [aweight=weight], robust cluster(school_baseline)
	xi: reg `var' treatment `controls2' if insample==1 [aweight=weight], robust cluster(school_baseline)
	
}

restore


*************************
***APPENDIX TABLE A3*****
*************************


****************************************************
****rerun mean effects with interaction with indicator for 0 predicted winners
*****************************************************

foreach p in 10 20 30 40 50 60 70 80 90{
	bysort school_baseline: egen base_score_`p'=pctile(c1_score_2000), p(`p')
}

bysort school_baseline: egen winners=sum(win)
 gen a=win if c1==0
 gen b=win if c1==1
bysort school_baseline: egen winners1=sum(a)
bysort school_baseline: egen winners2=sum(b)
drop a b

egen tag_school=tag(school_baseline)
*one obs per school so as not to over-weight large schools

tobit winners base_score* if treatment==1&tag==1&teso==0, ll(0)
predict winners_predicted

replace winners_predicted=0 if winners_predicted<0
gen winners_predicted_treat=winners_predicted*treatment

******




foreach categ in wom dem polknowledge sat effic int{

local cat="``categ''"
local cat_name="`categ'"
foreach var in `cat'{
	sum `var' if treatment==0&insample==1
	replace `var'=(`var'-r(mean))/r(sd)
}

cap drop depvar_`categ'
egen depvar_`categ'=rmean(`cat') if insample==1
sum depvar_`categ' if insample==1
replace depvar_`categ'=(depvar_`categ'-r(mean))/r(sd)

***********************interaction with indicator for number of predicted winners
}

sum winners if tag_school==1&teso==0
gen winners_predicted_n = (winners_predicted-r(mean))
gen winners_pred_treat_n=treatment*winners_predicted_n

foreach v in wom dem polknowledge sat effic int{
xi: reg depvar_`v' treatment winners_pred_treat_n winners_predicted_n `controls' if insample==1 [aweight=weight], robust cluster(school_baseline)
}
foreach var in s13_12_tribe_import_r s13_13_religion_important s12_17newspaper supportforviolence1 supportforviolence2{
xi: reg `var' treatment winners_pred_treat_n winners_predicted_n `controls' if insample==1 [aweight=weight], robust cluster(school_baseline)
}


**************************
*************************

use edaslib_1999kenya_education.dta, clear

drop if age<15
gen yob = 1999-age
drop if age>900
gen primary = 1 if edattan==2|edattan==3|edattan==4
replace primary =0 if edattan ==1

gen yrseduc = yrschl if yrschl !=99 & yrschl !=98
replace yrseduc = 14 if yrschl==94
collapse (mean) primary yrseduc ,by(sex yob)










log close
